package org.xhy.sniffer.plugin;

import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import org.xhy.sniffer.interceptor.ConstructorMethodsInterceptorPoint;
import org.xhy.sniffer.interceptor.InstanceMethodsInterceptorPoint;
import org.xhy.sniffer.interceptor.StaticMethodsInterceptorPoint;
import org.xhy.sniffer.match.ClassMatch;

/**
 * @description: 插件顶级接口，用于获取拦截器，封装增强逻辑行为
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-07-11 19:38
 */
@Slf4j
public abstract class AbstractClassEnhancePluginDefine {

    // 获取匹配规则：匹配的类
    protected abstract ClassMatch enhanceClass();

    // 获取实例方法拦截器
    protected abstract InstanceMethodsInterceptorPoint[] getInstanceMethodsInterceptorPoints();

    // 获取静态方法拦截器
    protected abstract StaticMethodsInterceptorPoint[] getStaticMethodsInterceptorPoints();

    // 获取构造方法拦截器
    protected abstract ConstructorMethodsInterceptorPoint[] getConstructorMethodsInterceptorPoints();


    // 增强方法
    protected abstract DynamicType.Builder<?> enhanceMethod(TypeDescription typeDescription, DynamicType.Builder<?> builder, ClassLoader classLoader);

    public DynamicType.Builder<?> enhance(TypeDescription typeDescription, DynamicType.Builder<?> builder, ClassLoader classLoader){
        builder = this.enhanceMethod(typeDescription,builder,classLoader);
        return builder;
    }

    // 增强类主入口
    public DynamicType.Builder<?> define(TypeDescription typeDescription, DynamicType.Builder<?> builder, ClassLoader classLoader){

        final String typeName = typeDescription.getTypeName();
        log.info("{} 开始增强",typeName);
        DynamicType.Builder<?> newBuilder = this.enhance(typeDescription,builder,classLoader);
        log.info("{} 增强结束",typeName);
        return newBuilder;
    }

}
